package com.vshop.platform.system.crud.dao;

import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.vshop.framework.mybatisflex.page.PageQuery;
import com.vshop.platform.system.domain.entity.SysLoginLog;
import com.vshop.platform.system.crud.mapper.SysLoginLogMapper;
import com.vshop.platform.system.domain.param.SysLoginLogParam;
import org.springframework.stereotype.Repository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import static com.vshop.platform.system.domain.entity.table.SysLoginLogTableDef.SysLoginLogTable;


/**
 * 系统访问记录 服务层实现。
 *
 * @author vshop
 * @since 2024-11-18
 */
@RequiredArgsConstructor
@Slf4j
@Repository
public class SysLoginLogDao extends ServiceImpl<SysLoginLogMapper, SysLoginLog> {

    public QueryWrapper buildQueryWrapper(SysLoginLogParam param, PageQuery pageQuery) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .and(SysLoginLogTable.Ip.like(param.getIp()))
                .and(SysLoginLogTable.Status.eq(param.getStatus()))
                .and(SysLoginLogTable.UserName.like(param.getUserName()))
                .and(SysLoginLogTable.LoginTime.between(param.getParams().get("beginTime"),
                        param.getParams().get("endTime")));
//                .orderBy(SysLoginLogTable.Id.desc());
//        if (StrUtil.isNotBlank(pageQuery.getOrderByColumn())) {
//            queryWrapper.orderBy(pageQuery.buildOrderBy());
//        }
        if (StrUtil.isBlank(pageQuery.getOrderByColumn())) {
            queryWrapper.orderBy(SysLoginLogTable.Id.desc());
        } else {
            queryWrapper.orderBy(pageQuery.buildOrderBy());
        }
        return queryWrapper;
    }
}
